
/*
 * TextEditorFrame.java
 *
 * Created on Jan 7, 2010, 10:55:26 PM
 */

package dev.clipall.view;

import dev.clipall.Constants;
import dev.clipall.business.GenericMediator;
import dev.clipall.model.ExtendedItem;
import dev.clipall.model.Item;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

/**
 *
 * @author dev
 */
public class TextEditorFrame extends javax.swing.JFrame {

    private JPanel panel;
    private Item item;

    private static TextEditorFrame frame = new TextEditorFrame();
    
    /** Creates new form TextEditorFrame */
    public TextEditorFrame() {
        panel = new JPanel();
        setContentPane(panel);
        initComponents();
        setListeners();

        //setLocationRelativeTo(SearchFrame.getInstance());
        try {
            setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource(Constants.Resources.SYSTEM_TRAY_ICON)));
        } catch (Exception ex) {
        }        
    }

    public static TextEditorFrame getInstance(){
        return frame;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jItemField = new javax.swing.JTextArea();

        setTitle("Item Text");
        setAlwaysOnTop(true);

        jItemField.setColumns(20);
        jItemField.setRows(5);
        jScrollPane1.setViewportView(jItemField);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 462, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea jItemField;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    public void setListeners() {

        panel.registerKeyboardAction(new EscapeKeyActionListener(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JPanel.WHEN_IN_FOCUSED_WINDOW);
        panel.registerKeyboardAction(new PasteActionListener(), KeyStroke.getKeyStroke(KeyEvent.VK_P, InputEvent.CTRL_MASK), JPanel.WHEN_IN_FOCUSED_WINDOW);
        panel.registerKeyboardAction(new ChangeItemListener(), KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.CTRL_MASK), JPanel.WHEN_IN_FOCUSED_WINDOW);
    }

    public Item getItem() {
        return item;
    }

    public void setItem(Item item) {
        this.item = item;
    }

    class PasteActionListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {

            setVisible(false);
            ExtendedItem extItem = new ExtendedItem(item);
            extItem.setItem(jItemField.getText());
            GenericMediator.getInstance().pasteItemWithoutOrder(extItem);
        }
    }

    class ChangeItemListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            setVisible(false);
            item.setItem(jItemField.getText());            
        }

    }

    public void display(Item item) {

        if(item == null){
            return;
        }

        //this.item = new ExtendedItem(item);
        this.item = item;

        jItemField.setText(item.getItem());

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                setVisible(true);
            }
        });
    }

    class EscapeKeyActionListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            setVisible(false);
        }
    }
}
